<?php
// +----------------------------------------------------------------------
// | 数据库迁移 - 创建系统数据库
// +----------------------------------------------------------------------

use think\facade\Db;
use think\migration\Migrator;

class CreateSystem extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {

        // thinkphp无法做到创建数据库
        // if (strtolower(env('database.type')) === 'mysql') {
        //     Db::execute("CREATE DATABASE IF NOT EXISTS " . env('database.name') . " CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;");
        // }

        // 系统用户表
        $table = $this->table('system_users', ['engine' => 'MyISAM', 'collation' => 'utf8mb4_bin', 'comment' => '系统用户表']);
        $table->addColumn('username', 'string', ['limit' => 100, 'null' => false, 'comment' => '账号'])
            ->addColumn('nickname', 'string', ['limit' =>  100, 'null' => false, 'comment' => '昵称'])
            ->addColumn('password', 'string', ['limit' => 255, 'null' => false, 'comment' => '密码'])
            ->addColumn('phone', 'string', ['limit' => 100, 'default' => NULL, 'comment' => '手机号'])
            ->addColumn('email', 'string', ['limit' => 100, 'default' => NULL, 'comment' => '邮箱'])
            ->addColumn('remark', 'string', ['limit' => 255, 'default' => NULL, 'comment' => '备注'])
            ->addColumn('sort', 'integer', ['default' => 0, 'comment' => '排序'])
            ->addColumn('status', 'boolean', ['limit' => 1, 'default' => 1, 'comment' => '状态'])
            ->addColumn('create_time', 'datetime', ['default' => NULL, 'comment' => '创建时间'])
            ->addColumn('update_time', 'datetime', ['default' => NULL, 'comment' => '更新时间'])
            ->addIndex(['username'], ['unique' => true])
            ->create();

        // 系统用户消息表
        $table = $this->table('system_users_messages', ['engine' => 'InnerDB', 'collation' => 'utf8mb4_bin', 'comment' => '系统用户消息表']);
        $table->addColumn('user_id', 'integer', ['null' => false, 'comment' => '用户ID'])
            ->addColumn('title', 'string', ['limit' => 100, 'null' => false, 'comment' => '标题类型'])
            ->addColumn('description', 'string', ['limit' => 255, 'null' => false, 'comment' => '内容'])
            ->addColumn('is_read', 'boolean', ['default' => 1, 'comment' => '是否已读'])
            ->addColumn('create_time', 'datetime', ['default' => NULL, 'comment' => '创建时间'])
            ->addColumn('update_time', 'datetime', ['default' => NULL, 'comment' => '更新时间'])
            ->create();

        // 系统用户操作表
        $table = $this->table('system_users_logs', ['engine' => 'InnerDB', 'collation' => 'utf8mb4_bin', 'comment' => '系统用户操作表']);
        $table->addColumn('type', 'string', ['limit' => 20, 'null' => false, 'comment' => '类型'])
            ->addColumn('user_id', 'integer', ['null' => false, 'comment' => '用户ID'])
            ->addColumn('summary', 'string', ['limit' => 255, 'null' => false, 'comment' => '信息摘要'])
            ->addColumn('url', 'string', ['limit' => 100, 'default' => NULL, 'comment' => '请求地址'])
            ->addColumn('method', 'string', ['limit' => 100, 'default' => NULL, 'comment' => '请求类型'])
            ->addColumn('body', 'json', ['comment' => '请求参数'])
            ->addColumn('user_agent', 'string', ['limit' => 255, 'comment' => '请求UA'])
            ->addColumn('user_info', 'json', ['comment' => '用户详情'])
            ->addColumn('ip_address', 'string', ['limit' => 100, 'comment' => 'IP地址'])
            ->addColumn('ip_location', 'string', ['limit' => 100, 'comment' => 'IP归属'])
            ->addColumn('create_time', 'datetime', ['default' => NULL, 'comment' => '创建时间'])
            ->addColumn('update_time', 'datetime', ['default' => NULL, 'comment' => '更新时间'])
            ->create();

        // 系统用户与角色关联表
        $table = $this->table('system_users_roles', ['engine' => 'MyISAM', 'id' => false, 'collation' => 'utf8mb4_bin', 'comment' => '系统用户角色表']);
        $table->addColumn('user_id', 'integer', ['null' => false, 'comment' => '用户ID'])
            ->addColumn('role_id', 'integer', ['null' => false, 'comment' => '角色ID'])
            ->addIndex(['user_id', 'role_id'], ['unique' => true])
            ->create();

        // 系统用户与部门关联表
        $table = $this->table('system_users_depts', ['engine' => 'MyISAM', 'id' => false, 'collation' => 'utf8mb4_bin', 'comment' => '系统用户部门表']);
        $table->addColumn('user_id', 'integer', ['null' => false, 'comment' => '用户ID'])
            ->addColumn('dept_id', 'integer', ['null' => false, 'comment' => '部门ID'])
            ->addIndex(['user_id', 'dept_id'], ['unique' => true])
            ->create();

        // 系统角色表
        $table = $this->table('system_roles', ['engine' => 'MyISAM', 'collation' => 'utf8mb4_bin', 'comment' => '系统角色表']);
        $table->addColumn('role_name', 'string', ['limit' => 100, 'null' => false, 'comment' => '角色名'])
            ->addColumn('code', 'string', ['limit' => 100, 'null' => false, 'comment' => '角色标识'])
            ->addColumn('permissions', 'json', ['comment' => '菜单权限'])
            ->addColumn('remark', 'string', ['limit' => 255, 'default' => NULL, 'comment' => '备注'])
            ->addColumn('sort', 'integer', ['default' => 0, 'comment' => '排序'])
            ->addColumn('status', 'boolean', ['limit' => 1, 'default' => 1, 'comment' => '状态'])
            ->addColumn('create_time', 'datetime', ['default' => NULL, 'comment' => '创建时间'])
            ->addColumn('update_time', 'datetime', ['default' => NULL, 'comment' => '更新时间'])
            ->addIndex(['code'], ['unique' => true])
            ->create();

        // 系统部门表
        $table = $this->table('system_depts', ['engine' => 'MyISAM', 'collation' => 'utf8mb4_bin', 'comment' => '系统部门表']);
        $table->addColumn('parent_id', 'integer', ['null' => false, 'comment' => '父级ID'])
            ->addColumn('dept_name', 'string', ['limit' => 100, 'null' => false, 'comment' => '部门名称'])
            ->addColumn('principal', 'string', ['limit' => 100, 'default' => NULL, 'comment' => '部门负责人'])
            ->addColumn('phone', 'string', ['limit' => 100, 'default' => NULL, 'comment' => '手机号'])
            ->addColumn('email', 'string', ['limit' => 100, 'default' => NULL, 'comment' => '邮箱'])
            ->addColumn('remark', 'string', ['limit' => 255, 'default' => NULL, 'comment' => '备注'])
            ->addColumn('sort', 'integer', ['default' => 0, 'comment' => '排序'])
            ->addColumn('status', 'boolean', ['limit' => 1, 'default' => 1, 'comment' => '状态'])
            ->addColumn('create_time', 'datetime', ['default' => NULL, 'comment' => '创建时间'])
            ->addColumn('update_time', 'datetime', ['default' => NULL, 'comment' => '更新时间'])
            ->create();

        // 系统菜单表
        $table = $this->table('system_menus', ['engine' => 'MyISAM', 'collation' => 'utf8mb4_bin', 'comment' => '系统菜单表']);
        $table->addColumn('parent_id', 'integer', ['null' => false, 'comment' => '父级ID'])
            ->addColumn('type', 'string', ['limit' => 20, 'null' => false, 'comment' => '类型'])
            ->addColumn('menu_name', 'string', ['limit' => 100, 'null' => false, 'comment' => '菜单名称'])
            ->addColumn('route_name', 'string', ['limit' => 100, 'null' => false, 'comment' => '路由名称'])
            ->addColumn('path', 'string', ['limit' => 100, 'default' => NULL, 'comment' => '路由地址'])
            ->addColumn('component', 'string', ['limit' => 100, 'default' => NULL, 'comment' => '组件地址'])
            ->addColumn('frame_src', 'string', ['limit' => 100, 'default' => NULL, 'comment' => 'iframe地址'])
            ->addColumn('redirect', 'string', ['limit' => 100, 'default' => NULL, 'comment' => '路由重定向'])
            ->addColumn('icon', 'string', ['limit' => 100, 'default' => NULL, 'comment' => '菜单图标'])
            ->addColumn('options', 'integer', ['default' => 0, 'comment' => '控制'])
            ->addColumn('remark', 'string', ['limit' => 255, 'default' => NULL, 'comment' => '备注'])
            ->addColumn('sort', 'integer', ['default' => 0, 'comment' => '排序'])
            ->addColumn('status', 'boolean', ['default' => 1, 'comment' => '状态'])
            ->addColumn('create_time', 'datetime', ['default' => NULL, 'comment' => '创建时间'])
            ->addColumn('update_time', 'datetime', ['default' => NULL, 'comment' => '更新时间'])
            ->create();
    }
}
